<?php

/*
 * 问答活动 获取题目信息
 */
include "inc/public_header.php";

$uid = $_REQUEST["uid"];  //用户的uid

//随机抽取问答题
$sql = "select id,atype,acode,aindex,acontent from ac_question_info where atype=2 order by aindex asc";
$db->query($sql);
$queArr = array();
while($db->next_record()){
	$queArr[] = array(
		"id" 			=> $db->f("id")			//题目主键
		,"aindex" 		=> $db->f("aindex")		//题目索引
		,"acontent" 	=> $db->f("acontent")		//题目内容
	);
}
shuffle($queArr); //随机打乱数组

$ques = ""; //答题主键
//返回值
$res = "{\"completion\": [\"".$queArr[0]["acontent"]."\"],\"choiceQuestion\": [";
$wdQue = $queArr[0]["id"]; //问答题主键

//获取全部选择题
$sql = "select id,atype,acode,aindex,acontent,aresult from ac_question_info where atype=0 order by aindex asc";
$db->query($sql);
$queArr = array();			//题目信息素组
$optionsArr = array();		//题目选项数组
while($db->next_record()){
	$queArr[] = array(
		"id" 			=> $db->f("id")			//题目主键
		,"aindex" 		=> $db->f("aindex")		//题目索引
		,"acontent" 	=> $db->f("acontent")	//题目内容
	);
	//列出当前题目的选项信息
	$sql = "select id,pid,aindex,aoption from ac_question_options where pid=".$db->f("id")." order by aindex asc";
	$db1->query($sql);
	$oArr = array(); //对应题目的选项素组
	while($db1->next_record()){
		$oArr[] = 
			array(
				"id"		=> $db1->f("id")		//选项主键
				,"pid"		=> $db1->f("pid")		//题目主键
				,"aindex"	=> $db1->f("aindex")	//选项索引
				,"aoption"	=> substr($db1->f("aoption"),4,strlen($db1->f("aoption")))	//选项内容
			);
	}
	$optionsArr[$db->f("id")] = $oArr;
}
shuffle($queArr); //随机打乱数组

for($i=0;$i<9;$i++){
	if($i>0){
		$res = $res . ",";
		$ques = $ques . ",";
	}
	
	$ques = $ques . $queArr[$i]["id"];
	
	$res = $res . "{\"title\": \"".$queArr[$i]["acontent"]."\",\"option\": [";
	
	$opts = $optionsArr[$queArr[$i]["id"]]; //选项数组
	for($j=0;$j<count($opts);$j++){
		if($j>0){
			$res = $res . ",";
		}
		$res = $res . "\"" . $opts[$j]["aoption"] . "\"";
	}
	
	$res = $res . "]}";
}
$res = $res . "]}";

$ques = $ques . "," . $wdQue;

//将题目主键先提交到数据库
$sql = "select id from ac_question_user where acode='131202' and uid='".$uid."'";
//判断以前有没有取过题目
$db->query($sql);
if($db->next_record()){
	$sql = "update ac_question_user set qid='".$ques."',ctime=now() where acode='131202' and uid='".$uid."'";
}else{
	$sql = "insert into ac_question_user (uid,ctime,acode,aresult,aflag,qid,away) values(".$uid.",now(),'131202','0','0','".$ques."',1)";
}
$db->query($sql);
	
echo $res;
?>